const db = require("../../../db");

const register = (req, res) => {
  let { phone, password } = req.body;
  let sql = `select * from user where phoneNumber = '${phone}'`;
  db.query(sql, (err, result) => {
    if (err) return res.send({ code: 2, message: err.message });
    if (result.length !== 0)
      return res.send({ code: 1, message: "该手机号已注册，请登录" });
    let sql = `insert into user(phoneNumber, password) values('${phone}','${password}')`;
    db.query(sql, (err, result) => {
      if (err) return res.send({ code: 2, message: err.message });
      if (result.affectedRows) {
        let sql = `select * from user where phoneNumber='${phone}' and password='${password}'`;
        db.query(sql, (err, result) => {
          if (err) return res.send({ code: 2, message: err.message });
          if (result.length === 0)
            return res.send({ code: 1, message: "注册失败" });
          res.send({ code: 0, data: result[0], message: "注册成功" });
        });
      } else {
        res.send({ code: 1, message: "注册失败" });
      }
    });
  });
};

module.exports = register;
